<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/29
 * Time: 12:43
 */

namespace app\admin\controller;

use app\admin\helper\LangHelper;
use think\Controller;
use think\Config;
use think\Cookie;
use think\Request;
use think\Session;

class Base extends Controller
{

    /**
     * @var Request
     */
    public $request;

    public $lang_list;
    public $curr_lang;

    /*
     * 初始化
     * */
    public function _initialize(){
        //无权限跳转
        $this->request = Request::instance();
        $auth_flag = true;
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());
        $no_auth_list = Config::get("app.no_auth_list");
        $no_auth_list = strtolower(json_encode($no_auth_list));
        $no_auth_list = json_decode($no_auth_list,true);
        foreach ($no_auth_list as $c=>$a){
            if ($controller == $c && in_array($action,$a)){
                $auth_flag = false;
                break;
            }
        }
        if (empty(Session::get('username')) && $auth_flag){
            if($this->request->isAjax()){
                $code = ['code'=>302,'msg'=>'__re_login__'];
                exit(json_encode(LangHelper::convertLang($code)));
            }
            $this->redirect("login");
        }
        //加载多语言包
        $this->lang_list = Config::get('app.lang');
        $this->assign('lang_list',$this->lang_list);
        $curr_lang = Cookie::get('curr_lang');
        $this->curr_lang = empty($curr_lang)?'zh':$curr_lang;
        $this->assign('curr_lang',$this->curr_lang);
        $this->assign('curr_lang_name',$this->lang_list[$this->curr_lang]);
        $lang = Config::get('lang_'.$this->curr_lang);
        $this->assign('lang',$lang);
        $this->assign("__url",$this->request->url());
        //export 参数转化
        if (input("get.export/b")) {
            $this->request->post(input("get."));
        }

    }

}